package com.xtkj.accountjava.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xtkj.accountjava.entity.UserAccountEntity;
import com.xtkj.accountjava.entity.UserAccountMonthEntity;
import com.xtkj.accountjava.entity.WeekMonthYearEntity;
import com.xtkj.accountjava.mapper.UserAccountMapper;
import com.xtkj.accountjava.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserAccountServiceImpl implements UserAccountService {

    @Autowired
    UserAccountMapper userAccountMapper;

    @Override
    public UserAccountEntity addUserAccount(UserAccountEntity userAccountEntity) {
        Integer insert = userAccountMapper.insert(userAccountEntity);
        if(insert==0){
            return  null;
        }else {
            return  userAccountEntity;
        }
    }

    @Override
    public UserAccountEntity updateUserAccount(UserAccountEntity userAccountEntity) {
        Integer updateById = userAccountMapper.updateById(userAccountEntity);
        if(updateById==0){
            return  null;
        }
        return userAccountEntity;
    }

    @Override
    public List<UserAccountEntity> getAccountPage(String startTime, String endTime,
                                                  int userId,int userAcbookId,
                                                  int currPage,int pageSize
                                                  ) {
        Page<UserAccountEntity> page= new Page<>(currPage,pageSize);
        //按
        page.setDesc("create_time");
        return userAccountMapper.getAccountPage(
                page,
                startTime,endTime,userId,userAcbookId,(currPage-1)*pageSize,pageSize).getRecords();
    }
    @Override
    public List<UserAccountMonthEntity> getAccountMonthPage(String startTime, String endTime,
                                                            int userId, int userAcbookId
    ) {
        int yearNum=1971;
        int monthNum=01;
        int startDayNum=0;
        int endDayNum=0;
        try {
            SimpleDateFormat startOrginParse =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startOrginDate = startOrginParse.parse(startTime);
            //计算年份
            SimpleDateFormat yearDateFormat =new SimpleDateFormat("yyyy");
            String yearStr = yearDateFormat.format(startOrginDate);
            if(yearStr==null)yearStr="1971";
            yearNum=Integer.parseInt(yearStr);
            //计算月份
            SimpleDateFormat monthDateFormat =new SimpleDateFormat("MM");
            String monthStr = monthDateFormat.format(startOrginDate);
            if(monthStr==null)monthStr="01";
            monthNum=Integer.parseInt(monthStr);
            //开始天数

            SimpleDateFormat startDateFormat =new SimpleDateFormat("dd");
            String startDayStr = startDateFormat.format(startOrginDate);
            if(startDayStr==null)startDayStr="0";
            startDayNum=Integer.parseInt(startDayStr);
            //结束天数
            SimpleDateFormat endOrginParse =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date endOrginDate = endOrginParse.parse(endTime);
            SimpleDateFormat endDateFormat =new SimpleDateFormat("dd");
            String endDayStr = endDateFormat.format(endOrginDate);
            if(endDayStr==null)endDayStr="0";
            endDayNum=Integer.parseInt(endDayStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        String yearTemp=yearNum+"";
        String monthTemp=(monthNum+"").length()>1?(monthNum+""):("0"+monthNum);
        List<UserAccountMonthEntity> userAccountMonthEntities=new ArrayList<>();
        while (startDayNum<=endDayNum){
//            String dayTemp=(startDayNum+"").length()>1?(startDayNum+""):("0"+startDayNum);
            String dayTemp=(endDayNum+"").length()>1?(endDayNum+""):("0"+endDayNum);
            String startDayTemp = yearTemp+"-"+monthTemp+"-"+dayTemp+" 00:00:00";
            String endDayTemp = yearTemp+"-"+monthTemp+"-"+dayTemp+" 23:59:59";
            List<UserAccountEntity> accountLists = userAccountMapper.getAccountList(startDayTemp, endDayTemp, userId, userAcbookId);
            if(accountLists.size()>0){
                UserAccountMonthEntity accountMonth=new UserAccountMonthEntity();
                accountMonth.setDayTime(Integer.parseInt(dayTemp));
                accountMonth.setUserAccountLists(accountLists);
                userAccountMonthEntities.add(accountMonth);
            }
//            startDayNum=startDayNum+1;
            endDayNum=endDayNum-1;
        }
        return userAccountMonthEntities;
    }

    @Override
    public List<WeekMonthYearEntity> getWeekMonthYearAccounts(
            String startWeekTime, String endWeekTime,
            String startMonthTime, String endMonthTime,
            String startYearTime, String endYearTime,
            int userId, int userAcbookId) {
        List<WeekMonthYearEntity> weekMonthYearEntities=new ArrayList<>();

        //获取本周数据
        List<UserAccountEntity> weekAccountLists = userAccountMapper.getAccountList(startWeekTime, endWeekTime, userId, userAcbookId);
        //收入
        double weekIncomeSumMoneys = weekAccountLists.stream().filter(item->item.getAcbookType()!=0).map(item ->
                (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
        //支出
        double weekPayOutSumMoneys = weekAccountLists.stream().filter(item->item.getAcbookType()==0).map(item ->
                (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
        WeekMonthYearEntity weekEntity = new WeekMonthYearEntity();
        weekEntity.setType("本周");
        weekEntity.setIncome(weekIncomeSumMoneys);
        weekEntity.setOutPay(weekPayOutSumMoneys);
        weekMonthYearEntities.add(weekEntity);
        //获取本月数据
        List<UserAccountEntity> monthAccountLists = userAccountMapper.getAccountList(startMonthTime, endMonthTime, userId, userAcbookId);
        //收入
        double monthIncomeSumMoneys = monthAccountLists.stream().filter(item->item.getAcbookType()!=0).map(item ->
                (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
        //支出
        double monthPayOutSumMoneys = monthAccountLists.stream().filter(item->item.getAcbookType()==0).map(item ->
                (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
        WeekMonthYearEntity monthEntity = new WeekMonthYearEntity();
        monthEntity.setType("本月");
        monthEntity.setIncome(monthIncomeSumMoneys);
        monthEntity.setOutPay(monthPayOutSumMoneys);
        weekMonthYearEntities.add(monthEntity);
        //获取本年数据
        List<UserAccountEntity> yearAccountLists = userAccountMapper.getAccountList(startYearTime, endYearTime, userId, userAcbookId);
        //收入
        double yearIncomeSumMoneys = yearAccountLists.stream().filter(item->item.getAcbookType()!=0).map(item ->
                (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
        //支出
        double yearPayOutSumMoneys = yearAccountLists.stream().filter(item->item.getAcbookType()==0).map(item ->
                (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
        WeekMonthYearEntity yearEntity = new WeekMonthYearEntity();
        yearEntity.setType("本年");
        yearEntity.setIncome(yearIncomeSumMoneys);
        yearEntity.setOutPay(yearPayOutSumMoneys);
        weekMonthYearEntities.add(yearEntity);
        return weekMonthYearEntities;
    }

    @Override
    public List<WeekMonthYearEntity> getMonthTrendAccounts(String startMonthTime, String endMonthTime, int userId, int userAcbookId) {
        int yearNum=1971;
        int monthNum=01;
        int startDayNum=0;
        int endDayNum=0;
        try {
            SimpleDateFormat startOrginParse =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startOrginDate = startOrginParse.parse(startMonthTime);
            //计算年份
            SimpleDateFormat yearDateFormat =new SimpleDateFormat("yyyy");
            String yearStr = yearDateFormat.format(startOrginDate);
            if(yearStr==null)yearStr="1971";
            yearNum=Integer.parseInt(yearStr);
            //计算月份
            SimpleDateFormat monthDateFormat =new SimpleDateFormat("MM");
            String monthStr = monthDateFormat.format(startOrginDate);
            if(monthStr==null)monthStr="01";
            monthNum=Integer.parseInt(monthStr);
            //开始天数

            SimpleDateFormat startDateFormat =new SimpleDateFormat("dd");
            String startDayStr = startDateFormat.format(startOrginDate);
            if(startDayStr==null)startDayStr="0";
            startDayNum=Integer.parseInt(startDayStr);
            //结束天数
            SimpleDateFormat endOrginParse =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date endOrginDate = endOrginParse.parse(endMonthTime);
            SimpleDateFormat endDateFormat =new SimpleDateFormat("dd");
            String endDayStr = endDateFormat.format(endOrginDate);
            if(endDayStr==null)endDayStr="0";
            endDayNum=Integer.parseInt(endDayStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        String yearTemp=yearNum+"";
        String monthTemp=(monthNum+"").length()>1?(monthNum+""):("0"+monthNum);
        List<WeekMonthYearEntity> userAccountMonthEntities=new ArrayList<>();
        while (startDayNum<=endDayNum){
            String dayTemp=(startDayNum+"").length()>1?(startDayNum+""):("0"+startDayNum);
            String startDayTemp = yearTemp+"-"+monthTemp+"-"+dayTemp+" 00:00:00";
            String endDayTemp = yearTemp+"-"+monthTemp+"-"+dayTemp+" 23:59:59";
            List<UserAccountEntity> accountLists = userAccountMapper.getAccountList(startDayTemp, endDayTemp, userId, userAcbookId);
            //收入总额
            double dayIncomeSumMoneys = accountLists.stream().filter(item->item.getAcbookType()!=0).map(item ->
                    (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();
            //支出总额
            double dayPayOutSumMoneys = accountLists.stream().filter(item->item.getAcbookType()==0).map(item ->
                    (Double.parseDouble(item.getMoney()==null?"0":item.getMoney()))).collect(Collectors.toList()).stream().mapToDouble(t->t).sum();

            WeekMonthYearEntity yearEntity = new WeekMonthYearEntity();
            yearEntity.setType(dayTemp);
            yearEntity.setIncome(dayIncomeSumMoneys);
            yearEntity.setOutPay(dayPayOutSumMoneys);
            userAccountMonthEntities.add(yearEntity);
            startDayNum=startDayNum+1;
        }
        return userAccountMonthEntities;
    }


}
